//
//  ShangHuViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/31.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "ShangHuViewController.h"

#import "SHView.h"
#import "GXDPView.h"
#import "PinJiaCell.h"
#import "PingJiaViewController.h"
#import "SHDetViewController.h"

#import "LoginViewController.h"
#import "NavViewController.h"
@interface ShangHuViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
    NSMutableDictionary *dic;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation ShangHuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    array = [NSMutableArray array];
    dic = [NSMutableDictionary dictionary];
    [self setBGColor];
    [self showNav:@"店铺详情"];
    [self loadUI];
    [self loadData2];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-50) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    GXDPView *view = [[[NSBundle mainBundle] loadNibNamed:@"GXDPView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 300);
    _tableView.tableHeaderView = view;
    
    UIView *v = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 0, ScreenWidth, 170) andImageArray:_images andBtnClick:^(NSInteger tagValue) {
        
    }];
    [view.images addSubview:v];
    
    view.labName.text = _name;
    view.labdet.text = _det;
    view.labPlace.text = [NSString stringWithFormat:@"地址:%@",_place];
    view.labOrder.text = _order;
    
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    NSInteger count = [_stats integerValue];
    starView.showStar = count * 20.0;
    [view.starView addSubview:starView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.layer.cornerRadius = 20.0;
    btn.frame = CGRectMake(20, ScreenHeight-55, ScreenWidth-40, 40);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"我要上户" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickHJ) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

- (void)clickHJ{
    if (![user objectForKey:@"userid"]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        NavViewController *nav = [[NavViewController alloc] initWithRootViewController:login];
        nav.navigationBarHidden = YES;
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    
    SHDetViewController *det = [[SHDetViewController alloc] init];
    det.merid = _merid;
    det.name = _name;
    det.lat = _lat;
    det.lng = _lng;
    [self.navigationController pushViewController:det animated:YES];
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary *op = array[indexPath.row];
    cell.labName.text = [op objectForKey:@"nickname"];
    cell.labText.text = [op objectForKey:@"comment"];
    NSString *time = [op objectForKey:@"commenttime"];
    NSString *date = [self getNewDate:time];
    cell.labDate.text = date;
    
    NSString *star = [op objectForKey:@"star"];
    CGFloat count = [star floatValue];
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    starView.showStar = count * 20;
    [cell.starView addSubview:starView];
    
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"avatar"]];
    cell.images.layer.cornerRadius = 11.5;
    cell.images.layer.masksToBounds = YES;
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 100, 20)];
    NSInteger indexs = array.count;
    lab.text = [NSString stringWithFormat:@"总评价：%li",indexs];
    lab.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:lab];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(16, 39, ScreenWidth, 0.5)];
    line.backgroundColor = [UIColor grayColor];
    [view addSubview:line];
    
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(ScreenWidth-80, 10, 80, 15)];
    NSString *count = [[[dic objectForKey:@"result"] objectForKey:@"commentcount"] objectForKey:@"evaluate"];
    NSInteger index = [count integerValue];
    starView.showStar = index*20;
    [view addSubview:starView];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

//尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, ScreenWidth, 40.0);
    [btn setTitle:@"查看全部评价" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2+35, 14, 60, 12)];
    NSInteger indexs = array.count;
    lab.text = [NSString stringWithFormat:@" (%li)",indexs];
    lab.font = [UIFont systemFontOfSize:12.0];
    lab.textColor = [UIColor redColor];
    [view addSubview:lab];
    
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40.0;
}

- (void)click{
    PingJiaViewController *pingjia = [[PingJiaViewController alloc] init];
    pingjia.merid = _merid;
    pingjia.url = [NSString stringWithFormat:@"%@/App/Merchantcomment/onthdoor",urlHead];
    [self.navigationController pushViewController:pingjia animated:YES];
}

//上户评论
- (void)loadData2{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    [parmas setObject:@"1" forKey:@"p"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Merchantcomment/onthdoor",urlHead];
    [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"上户评论res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            array = [[responseObject objectForKey:@"result"] objectForKey:@"comment"];
            dic = [responseObject objectForKey:@"result"];
            [_tableView reloadData];
        }
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
